﻿using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTab;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
//using System.Linq;
using System.Security.Cryptography;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;

namespace 系统通用数据检查规则
{
    public class Cpublic
    {
        /// <summary>
        /// 模块常量
        /// </summary>
        public static DevExpress.XtraTab.XtraTabControl XTC;

        public static Boolean ClosePage()
        {

            if (XTC.TabPages.Count == 1) return false;
            if (XTC.SelectedTabPage.Text == "首页") return false;
            DevExpress.XtraTab.XtraTabPage xtp = null;
            try
            {
                xtp = XTC.SelectedTabPage;
                XTC.SelectedTabPageIndex = XTC.SelectedTabPageIndex - 1;
            }
            catch { }
            try
            {
                xtp.Controls[0].Dispose();
                XTC.TabPages.Remove(xtp);
                xtp.Dispose();
                return true;
            }
            catch { }
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fm"></param>
        /// <param name="Caption"></param>
        public static void AddNewPage(UserControl fm, string Caption)
        {
            XtraTabPage xtp = XTC.TabPages.Add(Caption);
            xtp.ShowCloseButton = DefaultBoolean.Default;
            xtp.Controls.Add(fm);
            fm.Dock = DockStyle.Fill;
            XTC.SelectedTabPage = xtp;
        }
        /// <summary>
        /// 显示页面，如果已经存在
        /// </summary>
        /// <param name="fm">传入用户控件</param>
        /// <param name="Caption">传入字符串</param>
        public static void ShowNewPage(UserControl fm, string Caption)// 定义函数
        {
            foreach (XtraTabPage x in XTC.TabPages)// 便利控件xtratabpages集合； 
            {
                foreach (Control c in x.Controls)//便利 xtratabpage里面每个控件
                {
                    if (c.GetType() == fm.GetType())//  判断fm与  控件里的是不是同类型
                    {
                        XTC.SelectedTabPage = x;//  选中X显示； 
                        return;//返回
                    }
                }
            }
            XtraTabPage xtp = XTC.TabPages.Add(Caption);//创建新的XtraTabPage（添加名字）
            xtp.ShowCloseButton = DefaultBoolean.Default;

            xtp.Controls.Add(fm);//XtraTabPage里添加fm用户控件
            fm.Dock = DockStyle.Fill; //填充
            XTC.SelectedTabPage = xtp;   //选中xtp显示
        }

        public static System.Data.DataRow GetCurrDataRow(RepositoryItemSearchLookUpEdit rgu, object value)
        {
            if (rgu.DataSource is System.Data.DataTable)
            {
                System.Data.DataTable dt = (System.Data.DataTable)rgu.DataSource;
                return dt.DefaultView[rgu.GetIndexByKeyValue(value)].Row;
            }
            else
            {

                System.Data.DataView dt = (System.Data.DataView)rgu.DataSource;
                return dt[rgu.GetIndexByKeyValue(value)].Row;
            }
        }



        #region 全局变量

        /// <summary>
        /// 当前用户名
        /// </summary>
        public static string localUserName = "";

        /// <summary>
        /// 当前用户ID
        /// </summary>
        public static string LocalUserID = "";

        /// <summary>
        /// 用户权限组
        /// </summary>
        public static string LocalUserTeam = "";

        public static string FileServerConnString = "";
        /// <summary>
        /// 服务器名
        /// </summary>
        public static string ServerName = "";
        /// <summary>
        /// 服务器IP
        /// </summary>
        public static string ServerIP = "";
        /// <summary>
        /// 服务器端口
        /// </summary>
        public static int ServerListenPort = 0;
        /// <summary>
        /// 通道名称
        /// </summary>
        public static String ChannelName = null;

        #endregion

        #region 当前路径
        public static string strDir = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

        #endregion

        #region 私有变量
        /// <summary>
        /// 数据库连接串
        /// </summary>
        private static string strconn = "";

        /// <summary>
        /// 数据库连接串_FC
        /// </summary>
        private static string strconn_fc = "";

        /// <summary>
        /// web service 连接地址
        /// </summary>
        private static string strWSconn = "";

        /// <summary>
        /// web service 连接地址
        /// </summary>
        private static string strconn_fs = "";


        private static Dictionary<string, string> li_CFG = null;
        #endregion

        public static string strConn
        {
            get
            {
                fun_初始化参数();
                if (strconn == "")
                {
                    if (li_CFG["UID"] == "")
                    {
                        strconn = string.Format("Initial Catalog={0};Data Source={1};Integrated Security=True;Pooling=true;Max Pool Size=40000;Min Pool Size=0", li_CFG["DataBase"], li_CFG["SQLServer"]);
                        ServerName = li_CFG["SQLServerName"].ToString();
                        ServerIP = li_CFG["SQLServer"].ToString();
                        try
                        {
                            ServerListenPort = int.Parse(li_CFG["ServerPort"].ToString());
                        }
                        catch { }
                    }
                    if (li_CFG["UID"] != "")
                    {
                        strconn = string.Format("Password={0};Persist Security Info=True;User ID={1};Initial Catalog={2};Data Source={3};Pooling=true;Max Pool Size=40000;Min Pool Size=0",
                            li_CFG["PWD"], li_CFG["UID"], li_CFG["DataBase"], li_CFG["SQLServer"]);
                        ServerName = li_CFG["SQLServerName"].ToString();
                        ServerIP = li_CFG["SQLServer"].ToString();
                        try
                        {
                            ServerListenPort = int.Parse(li_CFG["ServerPort"].ToString());
                        }
                        catch { }
                    }
                }
                return strconn;
            }
        }

        private static void fun_初始化参数()
        {
            try
            {
                if (li_CFG == null)
                {
                    li_CFG = new Dictionary<string, string>();
                    string sKey = "MES_keys";
                    string path = strDir + "\\conn.cfg";
                    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                    DES.Key = ASCIIEncoding.UTF8.GetBytes(sKey);
                    DES.IV = ASCIIEncoding.UTF8.GetBytes(sKey);
                    ICryptoTransform desencrypt = DES.CreateDecryptor();
                    byte[] data = File.ReadAllBytes(path);
                    byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
                    MemoryStream ms = new MemoryStream(result);
                    DataTable dtM = new DataTable();
                    dtM.ReadXml(ms);
                    foreach (DataRow r in dtM.Rows)
                    {
                        li_CFG.Add(r[0].ToString(), r[1].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}
